package com.cwnu.controller;

import com.cwnu.entity.Remark;
import com.cwnu.entity.Result;
import com.cwnu.service.RemarkService;
import com.cwnu.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/remark")
public class RemarkController {
    @Autowired
    private RemarkService remarkService;
    // 新增remark
    // 参数remark对象：包括article_id和审核类型type以及审核内容content,其余的都可根据对应关系生成
    @PostMapping("/add")
    public Result addRemark(@RequestBody Remark remark) {
        // 先根据remark的article_id生成对应的user_id
        // 查询user_id
        Integer articleId = remark.getArticleId();
        Integer userId = remarkService.getUserId(articleId);
        remark.setUserId(userId);
        remark.setCreateTime(LocalDateTime.now());
        boolean flag = remarkService.add(remark);
        if (flag) return Result.success();
        else return Result.error("插入失败，请重试");
    }
    // 查询remark: 对用户来说，是查询当前用户的所有remark，remark中包含对应的文章
    @GetMapping
    public Result getRemarkList() {
        Map<String, Object> mp = ThreadLocalUtil.get();
        Integer userId = (Integer) mp.get("id");
        List<Remark> remarkList = remarkService.getRemarkList(userId);
        return Result.success(remarkList);
    }
}
